
#include "History.h"

History::History(Scene^ project,Selector^ selector,System::Windows::Forms::ContextMenuStrip^ _menuStrip)
{
	this->proj = project;
	this->select = selector;
	this->menuStrip = _menuStrip;
	this->num = 0;
	this->pos = 0;
	this->hist = gcnew array<Scene^>(100);
	this->sel = gcnew array<Selector^>(100);

	this->hist[this->num] = gcnew Scene();
	this->hist[this->num]->Preload(this->menuStrip);
	this->hist[this->num]->MakeEqual(this->proj);
	this->sel[this->num] = gcnew Selector(this->hist[this->num]);
	this->sel[this->num]->MakeEqual(this->select);
	this->pos = this->num;
	this->num++;
}

int History::AddStep()
{
	DelToPos();
	this->hist[this->num] = gcnew Scene();
	this->hist[this->num]->Preload(this->menuStrip);
	this->hist[this->num]->MakeEqual(this->proj);
	this->sel[this->num] = gcnew Selector(this->hist[this->num]);
	this->sel[this->num]->MakeEqual(this->select);
	this->pos = this->num;
	this->num++;
	return this->num;
}
int History::DelToPos()
{
	int j=0;
	this->num = this->pos+1;
	return this->num;
}
int History::UpInHistory()
{
	this->pos++;
	if (this->pos >= this->num)
	{
		this->pos = this->num - 1;
	}
	return this->pos;
}
int History::DownInHistory()
{
	this->pos--;
	if (this->pos < 0)
	{
		this->pos = 0;
	}
	return this->pos;
}
int History::InvolveHistory()
{
	this->select->MakeEqual(this->sel[this->pos]);
	this->proj->MakeEqual(this->hist[this->pos]);
	return this->pos;
}
char* History::convertString(String^ other)
{
	// Pin memory so GC can't move it while native function is called
	pin_ptr<const wchar_t> wch = PtrToStringChars(other);

	// Conversion to char* :
	// Can just convert wchar_t* to char* using one of the 
	// conversion functions such as: 
	// WideCharToMultiByte()
	// wcstombs_s()
	// ... etc
	size_t convertedChars = 0;
	size_t sizeInBytes = ((other->Length + 1) * 2);
	errno_t err = 0;
	char *ch = (char *)malloc(sizeInBytes);

	err = wcstombs_s(&convertedChars, ch, sizeInBytes, wch, sizeInBytes);
	return ch;
}
int History::get(char parameter)
{
	switch( parameter )
	{   
		case 'p': 
		case 'P':  
			return (int)this->pos;
			break;

		case 'n': 
		case 'N':  
			return (int)this->num;
			break; 

		default:
			break;
	}
}